<?PHP
require('core/Defaults.php');
require('core/SimpleIni.php');
require('core/Config.php');
require('core/TWError.php');
require('core/Resource.php');
require('core/Template.php');
require('core/Page.php');
require('core/Service.php');

class TeeWee {
	
	public static $CONFIG,$TEEWEE,$PAGE,$TEMPLATE;

	public static $DIR;

	private $clean = false;

	public function clean_exit() {
		$this->clean = true;
		exit();
	}
	public function is_clean() {
		return $this->clean;
	}

	private static function setup_globals() {
		self::$CONFIG		= new Config();
		self::$TEEWEE		= new TeeWee();
		self::$PAGE 		= Resource::create_from_request();
		self::$TEMPLATE	= new Template(self::$CONFIG->TEMPLATE_DEFAULT);
		self::$DIR			= getcwd();
		set_error_handler('TWError::error_handler');
		register_shutdown_function('TWError::shutdown');
	}

	/**
	 * The headers to be passed.
	 **/
	private $headers = array();

	/**
	 * The cookies to be set
	 **/
	private $cookies = array();

	/**
	 * Includes headers and cookies that were to be passed/set.
	 **/
	private function include_headers() {
		foreach($this->headers as $h)
			header($h[0],$h[1],$h[2]);
		foreach($this->cookies as $c)
			setcookie($c[0],$c[1],$c[2],$c[3],$c[4],$c[5],$c[6]);
	}

	// ---------------- //
	// -- Public API -- //
	// ---------------- //

	/**
	 * Start the application
		**/
	public static function launch() {
		self::setup_globals();
		ob_start();
		echo self::$TEMPLATE;
		$output = ob_get_clean();
		self::$TEEWEE->include_headers();
		echo $output;
		self::$TEEWEE->clean_exit();
//		$output = ob_get_contents();
//		echo $output;
		//self::$TEEWEE->include_headers();
	}

	/**
	 * Modifies the header. This can be done after other output is given as output is buffered.
	 **/
	public function header_full($string,$replace,$http_response_code) {
		array_push($header, array($string,$replace,$http_response_code));
	}

	/**
	 * Modifies the header. see above.
	 **/
	public function header($string) {
		$this->header($string,null,null);
	}

	/**
	 * Set a cookie. This can be done after other output is given as output is buffered.
	 **/
	public function cookie_full($name,$value,$expire,$path,$domain,$secure,$httponly) {
		array_push($cookies, array($name,$value,$expire,$path,$domain,$secure,$httponly));
	}

	/**
	 * Set a cookie, see above
	 **/
	public function cookie($name,$value,$expire) {
		$this->cookie($name,$value,$expire,null,null,false,false);
	}

	/**
	 * Halts the current page rendering and redirects to the
	 * given page on the same domain.
	 * @param $page The page to which we should redirect.
	 * @param $params Get parameters to be included in the redirect.
	 * These are escaped before fitted.
	 **/
	public function redirect_intern($page,$params) {
		$page = '/'.$page.'?'.self::$CONFIG->GET_REDIRECT;
		foreach($params as $k => $v)
			$page .= '&'.urlencode($k).'='.urlencode($v);
		header('Location: /'.$page);
		$this->clean_exit();
	}

	/**
	 * Halts the current page rendering and redirects to a new page
	 **/
	public function redirect_extern($url) {
		self::$TEEWEE->stop_request();
	}

	/**
	 * output this message before exitting after a crash report.
	 **/
	public function debug($msg) {
		array_push(TWError::$debug,$msg);		
	}

}
TeeWee::launch();
?>
